package com.tbit.main.service.manager.factory;


import com.alibaba.fastjson.JSONObject;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.dao.log.OperateLogDao;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.service.manager.spring.SpringUtils;
import com.tbit.main.util.LogUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TimerTask;

/**
 * 异步工厂（产生任务用）
 *
 * @author jing
 */
public class AsyncFactory {
    /**
     * 操作日志记录
     *
     * @return 任务task
     */
    public static TimerTask recordOper(final AccountUser accountUser, final OperateLog annotation, final Map<String, Object> nameValueMap,
                                       final Object obj, final String ip, final String requestURI) {
        return new TimerTask() {
            @Override
            public void run() {
                // 先打日志后面再入库
                LogUtil.info(String.format("[%s]:name:%s,phone:%s,userId:%s,type:%d,id:%d,module:%s,Method:%s,param:%s,ip:%s,obj:%s",
                        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis())), accountUser.getName(),
                        accountUser.getPhone(), accountUser.getAccountUserId(), accountUser.getAccountUserType(), accountUser.getAccountUserId(),
                        annotation.module().getName(), annotation.methods().getName(),
                        JSONObject.toJSONString(nameValueMap), ip, JSONObject.toJSONString(obj)));
                //记录日志
                com.tbit.main.pojo.OperateLog operateLog = new com.tbit.main.pojo.OperateLog(accountUser.getAccountUserType(), accountUser.getName(),
                        accountUser.getPhone(), accountUser.getAccountId(), new Date(), annotation.module().getName(), annotation.module().getSn(),
                        annotation.methods().getName(), annotation.methods().getSn(),
                        ip, requestURI, JSONObject.toJSONString(nameValueMap));
                SpringUtils.getBean(OperateLogDao.class).insertOperateLog(operateLog);
            }
        };
    }
}
